import { _decorator, Component, instantiate, Label, Node, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Bonus } from '../entity/Bonus';
const { ccclass, property } = _decorator;

@ccclass('GetBonus')
export class GetBonus extends Component {
  
    @property(Node)
    close:Node
    @property(Node)
    layout:Node
    @property(Node)
    item:Node
    @property(Node)
    confirm:Node

    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.confirm.on(Node.EventType.TOUCH_END, this.onClose, this)
    }

    async setData(list:Bonus[]){
        this.item.removeFromParent()

        for(let b of list){
            let node = instantiate(this.item)
            let img = node.getChildByName('img').getComponent(Sprite)
            let num = node.getChildByName('num').getComponent(Label)
            img.spriteFrame = await b.getImg()
            num.string = 'x ' + b.num 

            this.layout.addChild(node)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.GetBonus)
    }

    
}


